﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using BlawkayMobileLibrary;

namespace Bounce
{
    public class AimIt : GameType
    {
        public AimIt(GamePage oPage)
            : base(oPage)
        { }

        #region Screen Methods

        public override void Initialize(ContentManager oContentManager, GraphicsDevice oDevice)
        {
            base.Initialize(oContentManager, oDevice);

            int Height = 1000;
            Wall = new Rectangle(0, -Height, (int)Resolution.X, (int)Resolution.Y + Height);
            
            Texture2D texture_Basketball = oContentManager.Load<Texture2D>("Circle");
            Texture2D texture_Rim = oContentManager.Load<Texture2D>("Circle");
            Texture2D texture_AimLine = oContentManager.Load<Texture2D>("Square");

            float fRadius = 30.0f;
            float fMass = 2.0f;

            StartLocation = new Vector2(Wall.Center.X, Wall.Bottom - fRadius);
            Aimer = new AimLine(texture_AimLine);
            Aimer.Position_A = StartLocation;
            Aimer.Position_B = StartLocation;
            BasketBalls.Add(new Basketball(texture_Basketball, StartLocation, fRadius, fMass));
            Goals.Add(new Goal(new Vector2(240, 300), texture_Rim));
        }

        protected override void DrawGameType()
        {
            if (m_eShotStage == ShotStage.Aim)
            {
                Aimer.Draw(SpriteBatch);
            }
        }

        #endregion

        protected override void CheckCollisions()
        {
            base.CheckCollisions_Goals();
            base.CheckCollisions_Walls();
        }

        protected override void Input()
        {
            TouchCollection oCollection = TouchPanel.GetState();

            switch (m_eShotStage)
            {
                case ShotStage.Ready:
                    foreach (TouchLocation location in oCollection)
                    {
                        if (Circle.HasCollisionBetween(TheBall.Position, TheBall.Radius, location.Position, 20.0f))
                        {
                            Aimer.Position_A = StartLocation;
                            Aimer.Position_B = StartLocation;
                            m_eShotStage = ShotStage.Aim;
                            break;
                        }
                    }
                    break;

                case ShotStage.Aim:

                    if (oCollection.Count == 0)
                    {
                        Vector2 vNewVelocity = Aimer.Position_B - Aimer.Position_A;
                        TheBall.Velocity = vNewVelocity * 0.12f;
                        m_eShotStage = ShotStage.Fired;
                    }
                    else
                    {
                        foreach (TouchLocation location in oCollection)
                        {
                            Aimer.Position_B = location.Position;
                        }
                    }
                    break;

                case ShotStage.Fired:
                    foreach (TouchLocation location in oCollection)
                    {
                        ResetBasketball();
                        m_eShotStage = ShotStage.Ready;
                    }
                    break;
            }

        }

        #region Helper Methods

        void ResetBasketball()
        {
            TheBall.Reset(StartLocation);
        }

        #endregion

        Basketball TheBall
        {
            get { return BasketBalls[0]; }
        }

        AimLine Aimer
        {
            get;
            set;
        }

        #region Fields

        Vector2 StartLocation;

        //protected Camera2D Camera
        //{
        //    get;
        //    set;
        //}

        ShotStage m_eShotStage = ShotStage.Ready;
        enum ShotStage { Ready, Aim, Fired }

        #endregion

    }
}
